

*** find age distribution for different inpatient samples

use health/inp_clean, clear
merge m:1 sid using health/demographics, keep(1 3) assert(2 3) nogen


keep if admit_date >= mdy(3,13,2020)

gen all = 1 
gen has_dx = ~no_dx
gen no_icli = ~any_icli

gen c0 = " & " in 1
local c = 0

local glist all has_dx any_icli no_icli any_major ///
	cancer labor_delivery ami stroke fracture wound ///
	appendicitis vehicle_accident other_accident

qui foreach v in n female 0.age_wide 17.age_wide 29.age_wide 49.age_wide ///
	64.age_wide 74.age_wide 75.age_wide {
	
	local ++c
	local row = 1 
	gen c`c' = "`v' & " in `row'
	
	foreach g of local glist {
		local gname "`g'"
		
		if "`g'" == "all" 							local gname "All"
		if "`g'" == "has_dx" 						local gname "Has diagnosis"
		if "`g'" == "any_icli" 					local gname "ICLI"
		if "`g'" == "no_icli" 					local gname "Non-ICLI"
		if "`g'" == "any_major" 				local gname "Clear cause"
		if "`g'" == "cancer" 						local gname "Cancer"
		if "`g'" == "labor_delivery" 		local gname "Labor/delivery"
		if "`g'" == "ami" 							local gname "AMI"
		if "`g'" == "stroke" 						local gname "Stroke"
		if "`g'" == "fracture" 					local gname "Fracture"		
		if "`g'" == "wound" 						local gname "Open wound"
		if "`g'" == "appendicitis" 			local gname "Appendicitis"
		if "`g'" == "vehicle_accident"	local gname "Vehicle accident"
		if "`g'" == "other_accident" 		local gname "Other accident"
		
		
		local ++row
		replace c0 = "`gname' &" in `row'
		if "`v'" == "n" {
			count if `g'==1
			replace c`c' = string(`r(N)', "%10.0fc") + " & " in `row'
		}
		if "`v'" ~= "n"{
			sum `v' if `g'==1
			replace c`c' = string(`r(mean)', "%4.3f") + " & " in `row'
		}
		
	}	
}

replace c`c' = subinstr(c`c', "&", "\\",.)
list c0-c`c' in 1/`row', noobs clean
outsheet c0-c`c' in 2/`row' using tables/hosp_ss_by_group.tex, replace ///
	noquote nonames delimit(" ")
